package com.dongge.action;

import com.dongge.entity.Article;
import com.dongge.entity.Comment;
import com.dongge.service.ArticleService;
import com.dongge.service.CommentService;
import com.dongge.service.Page;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * Created by 朱东 on 2015/10/17.
 */

@Controller
@Scope("prototype")
public class ArticleAction extends BaseAction<Article> implements CURDAction{


    private int targetPage = -1;//接收页面提交的要显示哪一页数据

    public int getTargetPage() {
        return targetPage;
    }

    public void setTargetPage(int targetPage) {
        this.targetPage = targetPage;
    }

    @Resource
    private ArticleService articleService;

    @Resource
    private CommentService commentService;

    public String add() {

        model.setPublishTime(new Date());
        articleService.save(model);
        return "toList";
    }

    public String addPrepare() {
        return "addPrepare";
    }

    public String delete() {

        articleService.delete(model.getId());
        return "toList";
    }

    public String edit() {

        Article article  = articleService.get(model.getId());
        if(article ==null)
        {
            return "toList";
        }
        article.setContent(model.getContent());
        article.setTitle(model.getTitle());
        articleService.save(article);
        return "toList";
    }

    public String editPrepare() {

        Article article = articleService.get(model.getId());
        if(article == null){
            return "toList";
        }
        ActionContext.getContext().getValueStack().push(article);
        return "editPrepare";
    }

    /**
     * 获取文章列表 放在#articles中
     * @return
     */
    public String list() {
        Page page = articleService.getCurrentPage(targetPage,3);//每页显示三条数据
        ActionContext.getContext().getValueStack().push(page);
        return "list";
    }


}
